#include "MyPage.h"

MyPage::MyPage(QWidget *parent)
    : QWidget{parent}
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 上半部分：用户信息头部
    UserProfileHeader *header = new UserProfileHeader(this);
    mainLayout->addWidget(header, 1);

    // 添加切换按钮
    QWidget *buttonContainer = new QWidget(this);
    QHBoxLayout *buttonLayout = new QHBoxLayout(buttonContainer);

    buttonLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 左对齐 + 垂直居中

    QPushButton *blogButton = new QPushButton("文章违规历史", this);
    QPushButton *commentButton = new QPushButton("评论违规历史", this);

    blogButton->setFixedSize(BtnWidth, BtnHeight);
    commentButton->setFixedSize(BtnWidth, BtnHeight);

    // 添加按钮到布局中
    buttonLayout->addWidget(blogButton);
    buttonLayout->addWidget(commentButton);

    // 设置按钮容器的固定高度，防止拉伸
    buttonContainer->setFixedHeight(BtnHeight);

    // 设置按钮样式：圆角、边框、背景颜色等
    QString buttonStyle = R"(
        QPushButton {
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            border-radius: 10px;
            font-size: 14px;
            color: #333;
        }
        QPushButton:hover {
            background-color: #e0e0e0;
        }
        QPushButton:pressed {
            background-color: #d0d0d0;
        }
    )";

    blogButton->setStyleSheet(buttonStyle);
    commentButton->setStyleSheet(buttonStyle);

    mainLayout->insertWidget(1, buttonContainer, 0, Qt::AlignLeft); // 插入到 header 和 stackedWidget 之间，靠左对齐

    // 下半部分
    contentArea = new QStackedWidget(this);
    mainLayout->addWidget(contentArea, 5);

    blogListWidget = new BlogListWidget(this);
    commentListWidget = new CommentListWidget(this);

    contentArea->addWidget(blogListWidget);
    contentArea->addWidget(commentListWidget);

    setLayout(mainLayout);

    // 设置按钮激活样式
    QString activeStyle = R"(
        QPushButton {
            background-color: #4A90E2;
            border: 1px solid #357AE8;
            color: white;
            border-radius: 5px;
            font-size: 14px;
        }
    )";

    auto updateButtonStyles = [=](int currentIndex)
    {
        blogButton->setStyleSheet(currentIndex == 0 ? activeStyle : buttonStyle);
        commentButton->setStyleSheet(currentIndex == 1 ? activeStyle : buttonStyle);
    };

    // 初始设置第一个按钮为激活状态
    updateButtonStyles(0);

    // 连接按钮点击事件和 QStackedWidget 页面切换
    connect(blogButton, &QPushButton::clicked, [=]()
            { contentArea->setCurrentWidget(blogListWidget); });
    connect(commentButton, &QPushButton::clicked, [=]()
            { contentArea->setCurrentWidget(commentListWidget); });

    blogListWidget->load(DataAccess::getBlogList());
    commentListWidget->load(DataAccess::getCommentList());
}
